# -*- coding: utf-8; mode: Python; indent-tabs-mode: t -*-

# Copyright (C) 2013, 2014, 2019, 2022  Olga Yakovleva <olga@rhvoice.org>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

Import(["env"])

import os.path
import RHVoicePackaging.windows
import RHVoicePackaging.common

outdir=Dir("..").Dir("packages").Dir("sapi")

stable_version="1.16.4"

class core_packager(RHVoicePackaging.windows.msi_packager):
	def __init__(self,upgrade_code,x64_flag):
		self.x64_flag=x64_flag
		display_name="RHVoice ({}-bit)".format(64 if self.x64_flag else 32)
		name="RHVoice-"+self.get_arch()
		version=env["package_version"] if env["sapi_dev_core"] else stable_version
		super(core_packager,self).__init__(upgrade_code,name,outdir,env,display_name,version,"RHVoice")
		if not env["sapi_dev_core"]:
			return
		self.dll=Dir("..").Dir("x86_64" if x64_flag else "x86").Dir("sapi").File("RHVoiceSvr.dll")
		env.Depends(self.outfile,self.dll)
		self.ini=Dir("..").File("RHVoice.ini")
		env.Depends(self.outfile,self.ini)
		self.token_enum_key=r"Software\Microsoft\Speech\Voices\TokenEnums\RHVoice"
		self.token_enum_clsid="{d7577808-7ade-4dea-a5b7-ee314d6ef3a1}"
		self.engine_clsid="{9f215c97-3d3b-489d-8419-6b9abbf31ec2}"

	def is_64_bit(self):
		return self.x64_flag

	def get_parent_directory_id(self):
		return "ProgramFiles64Folder" if self.x64_flag else "ProgramFilesFolder"

	def get_old_lib_dir_path(self,win64):
		return "lib64" if win64 else "lib32"

	def create_com_server_component_element(self):
		cmp,file=self.create_file_component_element(RHVoicePackaging.common.file_info(self.dll,"lib",None,{}))
		for clsid in [self.engine_clsid,self.token_enum_clsid]:
			cls=self.SubElement(file,"Class",empty=True)
			cls.set("Id",clsid)
			cls.set("Context","InprocServer32")
			cls.set("ThreadingModel","both")
		return cmp

	def create_token_enum_component_element(self):
		dir=self.directory.find(RHVoicePackaging.windows.ns_wix+"Directory[@Name='lib']")
		cmp=self.SubElement(dir,"Component")
		cmp.set("Id","cmp_token_enum_clsid")
		cmp.set("Guid","*")
		cmp.set("Feature","Main")
		rv=self.SubElement(cmp,"RegistryValue",empty=True)
		rv.set("Action","write")
		rv.set("Id","reg_token_enum_clsid")
		rv.set("Key",self.token_enum_key)
		rv.set("KeyPath","yes")
		rv.set("Name","CLSID")
		rv.set("Root","HKLM")
		rv.set("Type","string")
		rv.set("Value",self.token_enum_clsid)

	def create_remove_old_reg_keys_component_element(self,win64):
		b=64 if win64 else 32
		cmp=self.SubElement(self.get_unique_subdirectory_element(),"Component")
		cmp.set("Id","cmp_rm_old_reg_keys_{}".format(b))
		cmp.set("Feature","Main")
		cmp.set("KeyPath","yes")
		cmp.set("Guid",self.make_uuid("Remove old registry keys: sapi5, {} bit (1)".format(b)))
		cmp.set("Win64","yes" if win64 else "no")
		cond=self.SubElement(cmp,"Condition")
		cond.text="NOT Installed"
		self.create_rm_reg_key(cmp,self.token_enum_key,id="rm_reg_key_token_enum_{}".format(b))
		self.create_rm_clsid_reg_key(cmp,self.token_enum_clsid)
		self.create_rm_clsid_reg_key(cmp,self.engine_clsid)

	def create_remove_old_lib_component_element(self,win64):
		lib_path=os.path.join(self.get_old_lib_dir_path(win64),"RHVoiceSvr.dll")
		self.create_remove_file_component_element(lib_path)

	def create_ini_component(self):
		dir=self.SubElement(self.root_directory,"Directory")
		dir.set("Id","CommonAppDataFolder")
		dir=self.SubElement(dir,"Directory")
		dir.set("Id","MyAppDataFolder")
		dir.set("Name",self.product.get("Manufacturer"))
		dir=self.SubElement(dir,"Directory")
		dir.set("Id","RHVoiceDataFolder")
		dir.set("Name","RHVoice")
		dir=self.SubElement(dir,"Directory")
		dir.set("Id","RHVoiceConfigFolder")
		dir.set("Name","config")
		cmp=self.SubElement(dir,"Component")
		file=self.SubElement(cmp,"File",empty=True)
		file.set("Id","RHVoiceIni")
		cmp.set("Id","cmp_"+file.get("Id"))
		cmp.set("Guid","*")
		cmp.set("Feature","Main")
		file.set("KeyPath","yes")
		file.set("Name","RHVoice.ini")
		file.set("Source",self.ini.abspath)

	def do_package(self):
		self.create_com_server_component_element()
		self.create_token_enum_component_element()
		self.create_remove_old_reg_keys_component_element(self.is_64_bit())
		self.create_remove_old_lib_component_element(self.is_64_bit())
		if not self.is_64_bit():
			self.create_remove_file_component_element(os.path.join("config-examples","RHVoice.ini"))
			self.create_remove_nsis_uninstaller_component_element()
		self.create_remove_folder_component_elements()
		self.create_ini_component()

msi32=core_packager("3794ffaa-7bc4-48cb-ae7d-67cb2560398a",False)
msi32.package()
env["WindowsInstallers_core_32"]=msi32
exe=RHVoicePackaging.windows.nsis_bootstrapper_packager("RHVoice",outdir,env,"RHVoice",msi32.version)
if env["enable_x64"]:
	msi64=core_packager("b48a841e-f35c-45e1-9206-a4be459fc0b2",True)
	msi64.package()
	env["WindowsInstallers_core_64"] = msi64
	exe.msis.append(msi64)
exe.msis.append(msi32)
exe.package()
